package com.imodia.checkoo.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.hsq.commons.util.db.GenericIbatisDao;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.annotation.SqlSpace;
import com.imodia.checkoo.bean.CmsArticleBean;
import com.imodia.checkoo.service.CmsArticleService;

@SqlSpace(spaceName = "cmsarticle")
@Service("cmsArticleService")
public class CmsArticleServiceImpl extends GenericIbatisDao<CmsArticleBean, Integer> implements CmsArticleService{
	
	private static final Log log = LogFactory.getLog(CmsArticleServiceImpl.class);

	public  PageResult<CmsArticleBean> queryForPageBySql(CmsArticleBean obj,PageBean page){
		String selectSql = "select distinct(id),model_id,title,short_content,content,insert_time,insert_user_id,update_time,update_user_id,author,public_date,is_validate,check_user_id,check_date,hit,publiser,rs_id,ext_1,ext_2,ext_3,ext_4,ext_5,ext_6,ext_7,ext_8,ext_9,ext_10,html_url,ext_11,ext_12,ext_13,ext_14,ext_15,ext_16,ext_17,ext_18,ext_19,ext_20,document_id,priority,link_url as dynamic_url  from v_cms_page_article where 1 =1 ";
		
		String sqlWhere = "";
		
		if(obj.getIsValidate()>0){
			sqlWhere += " and is_validate = "+obj.getIsValidate();
		}
		
		if(obj.getDocumentId()>0){
			sqlWhere += " and document_id = "+obj.getDocumentId();
		}
		
		if(obj.getCatalogueId()>0){
			sqlWhere += " and catalogue_id = "+obj.getCatalogueId();
		}
		
		if(obj.getTitle()!=null && obj.getTitle().trim().length()>0){
			sqlWhere += " and title like  '%"+obj.getTitle()+"%'";
		}
		
		selectSql += sqlWhere + " order by id desc";
		
		PageResult<CmsArticleBean> result = super.getSqlDao().page(selectSql,CmsArticleBean.class, page);
		return result;
		
	}
	
	public  List<CmsArticleBean> queryBySql(CmsArticleBean obj){
		String selectSql = "select distinct(id),title,insert_time,is_validate,link_url as dynamic_url from v_cms_article where 1 =1 ";
		String sqlWhere = "";
		
		if(obj.getIsValidate()>0){
			sqlWhere += " and is_validate = "+obj.getIsValidate();
		}

		if(obj.getDocumentId()>0){
			sqlWhere += " and document_id = "+obj.getDocumentId();
		}
		
		if(obj.getAticleSiteId()>0){
			sqlWhere += " and site_id = "+obj.getAticleSiteId();
		}
		
		if(obj.getCatalogueId()>0){
			sqlWhere += " and catalogue_id = "+obj.getCatalogueId();
		}
		
		if(obj.getTitle()!=null && obj.getTitle().trim().length()>0){
			sqlWhere += " and title like '%"+obj.getTitle().trim()+"%' ";
		}
		
		if(obj!=null && obj.getAreaName()!=null &&  obj.getAreaName().trim().length()>0){
			sqlWhere += " and area_name like '%"+obj.getAreaName().trim()+"%' ";
		}
		if(obj!=null && obj.getProvince()!=null &&  obj.getProvince()>0){
			sqlWhere += " and province = "+ obj.getProvince();
		}
		if(obj!=null && obj.getCity()!=null &&  obj.getCity()>0){
			sqlWhere += " and city = "+ obj.getCity();
		}
		
		selectSql += sqlWhere + " order by id desc  ";
		
		return super.getSqlDao().list(selectSql, CmsArticleBean.class);
		
	}
	
	
	public  void queryBySql(CmsArticleBean obj,Map<String,Object> model,PageBean page){
		String selectSql = "select distinct(id),title,insert_time,is_validate,link_url as dynamic_url from v_cms_article where 1 =1 ";
		String sqlWhere = "";
		
		if(obj.getIsValidate()!=null && obj.getIsValidate()!=-1){
			sqlWhere += " and is_validate = "+obj.getIsValidate();
		}
		
		
		if(obj.getDocumentId()!=null && obj.getDocumentId()>0){
			sqlWhere += " and document_id = "+obj.getDocumentId();
		}
		
		if(obj.getAticleSiteId()!=null && obj.getAticleSiteId()>0){
			sqlWhere += " and site_id = "+obj.getAticleSiteId();
		}
		
		if(obj.getCatalogueId()!=null && obj.getCatalogueId()>0){
			sqlWhere += " and catalogue_id = "+obj.getCatalogueId();
		}
		
		if(obj.getTitle()!=null && obj.getTitle().trim().length()>0){
			sqlWhere += " and title like '%"+obj.getTitle().trim()+"%' ";
		}
		
		
		if(obj!=null && obj.getAreaName()!=null &&  obj.getAreaName().trim().length()>0){
			sqlWhere += " and area_name like '%"+obj.getAreaName().trim()+"%' ";
		}
		if(obj!=null && obj.getProvince()!=null &&  obj.getProvince()>0){
			sqlWhere += " and province = "+ obj.getProvince();
		}
		if(obj!=null && obj.getCity()!=null &&  obj.getCity()>0){
			sqlWhere += " and city = "+ obj.getCity();
		}
		
		selectSql += sqlWhere + " order by id desc  ";
		
		log.debug(selectSql);

		PageResult<CmsArticleBean> result = super.getSqlDao().page(selectSql,CmsArticleBean.class, page);
		model.put("page", result);
		
	}
	
	public int checked(int checked ,int adminId,int id){
		String sql = "update cms_article set is_validate = "+checked+",update_time = now() , update_user_id = "+adminId+" where id = "+id;
		return super.getSqlDao().update(sql);
	}
	
	
	
	public List<CmsArticleBean> query(int documentId,int catalogueId,int startNum,int endNum){
		StringBuffer sql = new StringBuffer("select distinct(id),model_id,title,short_content,content,insert_time,insert_user_id,update_time,update_user_id,author,public_date,is_validate,check_user_id,check_date,hit,publiser,rs_id,ext_1,ext_2,ext_3,ext_4,ext_5,ext_6,ext_7,ext_8,ext_9,ext_10,html_url,ext_11,ext_12,ext_13,ext_14,ext_15,ext_16,ext_17,ext_18,ext_19,ext_20,document_id,priority,link_url as dynamic_url  from 	v_cms_article aa  where is_validate = 1 ");
		if(catalogueId>0){
			sql.append(" and aa.catalogue_id = "+catalogueId);
		}
		if(documentId>0){
			sql.append(" and aa.document_id = "+documentId);
		}
		sql.append(" order by aa.list_order ,end_date desc  limit "+startNum+","+endNum);
		List<CmsArticleBean> list = super.getSqlDao().list(sql.toString(), CmsArticleBean.class);
		return list;
	}
	
	
	public int updateHtml(String htmlUrl,int id){
		return super.getSqlDao().update("update cms_article set html_url ='"+htmlUrl+"'where id = "+id);
	}
	
}